<template>
	<view class="login-box">
		<image src="../../static/img/logo5.png" class="logo"></image>
		<button class="up-btn" open-type="getUserInfo" @getuserinfo="getuserinfo">点击登录</button>
	</view>
</template>

<script>
	import API from '../../api/index.js'
	export default {
		data() {
			return {
				
			}
		},
		methods: {
			// 获取用户头像
			getuserinfo(e) {
				console.log(e)
				// 用户同意授权
				if (e.detail.userInfo) {
					uni.login({
					  provider: 'weixin',
					  success: function (res) {
						  console.log(res.code)
						  
						  API.common.login({code:res.code,userInfo:e.detail.userInfo}).then(ret=>{
							  console.log(ret)
							  if (ret.errno=='0') {
								  console.log('登录成功')
								  uni.setStorageSync('token',ret.data.token)
								  uni.setStorageSync('userinfo',ret.data)
								  uni.showToast({
								      title: '登录成功',
								      duration: 2000
								  });
								  setTimeout(()=>{
									  uni.navigateBack({
									  	 delta: 1,
										 fail() {
											 uni.switchTab({
											 	url:'/pages/my/index'
											 })
										 }
									  })
								  })
							  }
							 
						  })
					
					  },
					  fail(){
						  console.log('失败')
					  }
					});
					
				} else {
					// 用户不同意授权
					uni.showModal({
					    title: '温馨提示',
					    content: '拒绝授权登录会无法操作相关操作哟',
					    success: function (res) {
					        if (res.confirm) {
					            console.log('用户点击确定');
					        } else if (res.cancel) {
					            console.log('用户点击取消');
					        }
					    }
					});
				}
			}
		}
	}
</script>

<style>
	.login-box {
		text-align: center;
	}
	.logo {
		width: 160rpx;
		height: 160rpx;
		border-radius: 50%;
		margin-top: 160rpx;
		margin-bottom: 120rpx;
	}
.up-btn {
	width: 702rpx;
	height: 80rpx;
	background: #333333;
	border-radius: 40rpx;
	margin: 15rpx auto;
	color: #fff;
	font-size: 30rpx;
	color: #F6CB90;
	line-height: 80rpx;
	text-align: center;
}
.up-btn:active {
	opacity: 0.7;
}
</style>
